package com.hanyoujia.http;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: TODO 类描述
 * @author: hanyoujia
 * @data 2021/7/28 9:04
 */
public class CloseableHttpClientUtil {

    /**
     * 以post方式调用第三方接口
     * @param url
     * @param json
     * @return
     */
    public static String doPost(String url, JSONObject json) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);

        //api_gateway_auth_token自定义header头，用于token验证使用
        //httpPost.addHeader("api_gateway_auth_token", tokenString);
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        try {
            StringEntity se = new StringEntity(json.toString(), Consts.UTF_8);
            se.setContentEncoding("UTF-8");
            se.setContentType("application/json");
            //设置请求参数
            httpPost.setEntity(se);
            HttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //返回json格式
                String res = EntityUtils.toString(response.getEntity());
                return res;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpClient != null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 以post方式调用第三方接口
     * @param url
     * @param json
     * @return
     */
    public static String doPostByForm(String url) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);

        //api_gateway_auth_token自定义header头，用于token验证使用
        //httpPost.addHeader("api_gateway_auth_token", tokenString);
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        try {

            List<NameValuePair> list = new ArrayList<NameValuePair>();
            list.add(new BasicNameValuePair("username", "aoc01"));
            list.add(new BasicNameValuePair("password", "bb0871c971688caa1a4ffd86883dd312"));

            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list, Consts.UTF_8);

            //发送json数据需要设置contentType
            formEntity.setContentType("application/x-www-form-urlencoded");

            //设置请求参数
            httpPost.setEntity(formEntity);
            HttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //返回json格式
                String res = EntityUtils.toString(response.getEntity());
                return res;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpClient != null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    @Test
    public void test(){
        String url = "http://localhost:8080/ecv/sys/login";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", "aoc01");
        jsonObject.put("password", "bb0871c971688caa1a4ffd86883dd312");

        String result = CloseableHttpClientUtil.doPost(url,jsonObject);
        System.out.println(result);
    }

    @Test
    public void test2(){
        String url = "http://localhost:8080/ecv/sys/logintest";
        String result = CloseableHttpClientUtil.doPostByForm(url);
        System.out.println(result);
    }

}
